package utils;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import components.RPSList;


/**
 * A GUI component to show the list of users.
 *
 * @see {@link utils.UserList}
 */

public class UserListGUI extends RPSList implements Observer {

	private static final long serialVersionUID = 1L;
	
	private UserList userList;

	
	public UserListGUI (UserList userList) {
		super ();			
		this.userList = userList;  
	    init();
	}

	
	private void init() {
		try
		{
			userList.addObserver (this);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	    refresh ();
	}
	
	public User getSelectedUser () {
		return (User)getSelectedValue();
	}
	
	public String getSelectedUsername () {
		return ((User)getSelectedValue()).getUsername();
	}	

	private void refresh () {
    	Vector<String> usersNames = userList.getUsersNames();	   	
		setListData (usersNames);
		repaint();
	}

    public void update (Observable obs, Object str) {
        refresh ();
    }
}
